﻿package vsg.engine {

	import flash.external.ExternalInterface;
	import flash.utils.*;
	import vsg.util.Runnable;
	import vsg.util.StringUtils;
	
	
	public class ClassObjectState implements ObjectState , Runnable{

		/**
		 * defined the state handle
		 */
		public static const LOADED:uint = 1;
		
		public static const UNLOADED:uint = 0;
		
		private var debug:Boolean = true;
		

		private var className:String;
		
		/**
		 * load the source into this variable
		 */
		private var classSource:String;
		
		/**
		 * defined the map state
		 */
		private var cState:uint = UNLOADED;
		
		/**
		 * defined the dependentClasses
		 */
		private var dependentClasses:Array = [];


		public function ClassObjectState(newClassName ) {
			this.className = newClassName;
		}
		
		public function getClassName():String {
			return this.className;
		}
		
		
		/**
		 * get the state handle
		 */
		public function getState():uint {
			return cState;
		}
		
		/**
		 * implement interface ObjectState.getClassSource 
		 */
		public function getClassSource():String {
			return this.classSource;
		}
		
		
		public function setState(newCstate:uint):void {
			this.cState = newCstate;
		}
		
		/**
		 * check dependent class array
		 * implement interface ObjectState.getClassSource 
		 */
		public function getDependentClasses():Array {
			return this.dependentClasses;
		}
		
		/**
		 *  implement interface Runnable.run
		 */
		public function run():void {
			
			var loadMark:Object = {"root":0 , "own":0};
			
			var fullPathName:String = StringUtils.replaceAll(className, "." , "/")+".js";
			
			// --- load the root file loader ---
			
			var fileLoader:UrlFileLoader = new UrlFileLoader("../../js/"+fullPathName);
			var rootDependentClasses:Array = [];
			var rootClassSource:String = "";
			// --- add load handle ---
			fileLoader.addAfterLoadHandle(function(args) {
				
				var $source = args.data;
				rootDependentClasses = ScriptLoaderGlobalHandle.findUsingImport($source);
				rootClassSource = ScriptLoaderGlobalHandle.scriptWithOutUsing($source);
				
				loadMark["root"] = 1;
				
			} , this);

			fileLoader.addLoadFailHandle(function(args) {
				
				
				loadMark["root"] = -1;
				
			} , this);			
			
			fileLoader.doLoad();
			
			
			// --- relative javascript class ---
			
			var rFileLoader:UrlFileLoader = new UrlFileLoader( "./js/" + fullPathName);
			var ownDependentClasses:Array = [];
			var ownClassSource:String = "";
			// --- add load handle ---
			rFileLoader.addAfterLoadHandle(function( args ) {
													 
				var $source = args.data;
				ownDependentClasses = ScriptLoaderGlobalHandle.findUsingImport($source);
				ownClassSource = ScriptLoaderGlobalHandle.scriptWithOutUsing($source);
				
				loadMark["own"] = 1;

			} , this);

			rFileLoader.addLoadFailHandle(function(args) {
				

				loadMark["own"] = -1;
				
			} , this);						
			
			rFileLoader.doLoad();
			
			
			
			
			// --- check the class status ---
			var loadComplete:Boolean = false;
			var timeMark = setInterval(function () {
				if (loadMark["root"] == 1) {
					
					dependentClasses = rootDependentClasses;
					classSource = rootClassSource;
					loadComplete = true;
					if (!debug) {
						clearInterval(timeMark);
					}
				} else if (loadMark["own"] == 1 ) {
					
					dependentClasses = ownDependentClasses;
					classSource = ownClassSource;
					loadComplete = true;
					if (!debug) {
						clearInterval(timeMark);
					}
				} else if (loadMark["own"] == -1 && loadMark["root"] == -1) {
					clearInterval(timeMark);
					loadComplete = true;
				}
				
				if (loadComplete) {
					// --- fire load complete ----
					setState( LOADED );
				}

			},50);
			
			
			
		}

		/**
		 *  implement interface ObjectState.chkJsObjectExist
		 */		
		public function chkJsObjectExist():Boolean {
			var $return = ExternalInterface.call("checkClassExist", className);
			return $return;
		}
		
		
		
		/*
		public function isObjExist():Boolean {
			var $return = ExternalInterface.call("checkClassExist", className);
			return $return;
		}
		*/

		
		
	}
	
}